package com.boxin.ims.modules.momarketing.utils;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.boxin.ims.modules.momarketing.entity.PageVisit;
import com.boxin.ims.modules.momarketing.service.PageVisitService;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.thinkgem.jeesite.modules.sys.entity.User;

@Service
public class PageVisitUtil   implements ApplicationContextAware {
	
	private static PageVisitService pageVisitService;
	

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		pageVisitService = (PageVisitService)applicationContext.getBean("pageVisitService");
		
	}
	
	
	public static void savePageVisit(HttpServletRequest request,User user,WechatMember member,String type,String pageId,String name){
		if(member==null) member = new WechatMember();
		
		PageVisit pageVisit=new PageVisit();
		pageVisit.setMobile(member.getMobile());
		pageVisit.setWechatId(member.getWechatId());
		pageVisit.setMemberName(member.getName());
		pageVisit.setUser(user);
		pageVisit.setType(type);
		pageVisit.setDetail(getHeader(request));
		pageVisit.setIp(getIpAddr(request));
		pageVisit.setName(name);
		pageVisitService.save(pageVisit);
	}
	
	
	public static String getHeader(HttpServletRequest request){
		String header="";
		header+="Protocol: " + request.getProtocol();
		header+="Scheme: " + request.getScheme();
		header+="Server Name: " + request.getServerName() ;
		header+="Server Port: " + request.getServerPort();
		header+="Protocol: " + request.getProtocol();
		header+="Remote Addr: " + request.getRemoteAddr();
		header+="Remote Host: " + request.getRemoteHost();
		header+="Character Encoding: " + request.getCharacterEncoding();
		header+="Content Length: " + request.getContentLength();
		header+="Content Type: "+ request.getContentType();
		header+="Auth Type: " + request.getAuthType();
		header+="HTTP Method: " + request.getMethod();
		header+="Path Info: " + request.getPathInfo();
		header+="Path Trans: " + request.getPathTranslated();
		header+="Query String: " + request.getQueryString();
		header+="Remote User: " + request.getRemoteUser();
		header+="Session Id: " + request.getRequestedSessionId();
		header+="Request URI: " + request.getRequestURI();
		header+="Servlet Path: " + request.getServletPath();
		header+="Accept: " + request.getHeader("Accept");
		header+="Host: " + request.getHeader("Host");
		header+="Referer : " + request.getHeader("Referer");
		header+="Accept-Language : " + request.getHeader("Accept-Language");
		header+="Accept-Encoding : " + request.getHeader("Accept-Encoding");
		header+="User-Agent : " + request.getHeader("User-Agent");
		header+="Connection : " + request.getHeader("Connection");
		header+="Cookie : " + request.getHeader("Cookie");
		return header;
	}
	
	
	public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
	
	
}
